/*
 * Copyright 2004 original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jbfilter.test.demo;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.jbfilter.bean.FilterBean;
import org.jbfilter.bean.PropertyAccessor;
import org.jbfilter.bean.factory.FilterBeans;
import org.jbfilter.bean.factory.FilterComponentBeans;
import org.jbfilter.core.FilterComponent;
import org.jbfilter.core.fcomps.multiple.StartEndDateFilterComponent;
import org.jbfilter.core.fcomps.single.ContainsStringFilterComponent;

	class Car {
		private String model;
		private Date registration;
		// constructor
		public Car(String model, Date registration) {
			super();
			this.model = model;
			this.registration = registration;
		}
		@Override
		public String toString() {
			return model + " " + registration;
		}
		// getter and setters
		public String getModel() {return model;}
		public void setModel(String model) {this.model = model;}
		public Date getRegistration() {return registration;}
		public void setRegistration(Date registration) {this.registration = registration;}
	}

	public class IntroExample {
	
		public static void main(String[] args) throws Exception {
		Collection<Car> cars = new ArrayList<Car>();
		SimpleDateFormat sdf = new SimpleDateFormat("MM/yyyy");
		cars.add(new Car("BMW", sdf.parse("08/2008")));// no this is not my car, unfortunately!
		cars.add(new Car("Peugeot", sdf.parse("05/1957")));// Colombo's car
		
		// create the filter :
		// always typed, this is a filter for cars
		FilterBean<Car> filter = FilterBeans.newFilter();
		// creates the filter components, typed like the filter
		
		// 1st possibility : use reflection to access property
		@SuppressWarnings("unused")
		FilterComponent<Car> fc1a = FilterComponentBeans.newContainsStringFilterComponent("model");
		
		// 2nd possibility (preferred) : create component with PropertyAccessor interface
		PropertyAccessor<Car, String> propAcc = new PropertyAccessor<Car, String>() {
			public String getPropertyValue(Car car) {
				return car.getModel();
			}
		};
		ContainsStringFilterComponent<Car> fc1b = FilterComponentBeans.newContainsStringFilterComponent("model", propAcc);
		
		// make the same for the second filter component
		StartEndDateFilterComponent<Car> fc2 = FilterComponentBeans.newStartEndDateFilterComponent("reg", new PropertyAccessor<Car, Date>() {
			public Date getPropertyValue(Car car) {
				return car.getRegistration();
			}
		});
		
		// attach the filter components to the filter
		filter.addFilterComponent(fc1b);
		filter.addFilterComponent(fc2);
		
		// set the components relevant settings
		fc1b.setValue("ugeo");
		fc2.setStartDate(sdf.parse("02/1948"));
		fc2.setEndDate(sdf.parse("07/1977"));
		
		// filter
		List<Car> filteredCars = filter.filter(cars);
		// contains only the Peugeot
		System.out.println(filteredCars);
	}

}
